<?php

namespace Biz\service\BrandRemind;

use Doraemon\tools\Sms;

class BrandSms implements IObserver
{
    const SEND_PROVIDER_AUTO = 0; //自动 需要配置联通、阿里模板

    const SEND_PROVIDER_ALI = 1; //阿里

    const SEND_PROVIDER_CNUNION = 2; //联通

    private $params = [];

    private $sendProvider = 0;

    public function notify()
    {
        //echo "BrandSms:" . var_export($this->params, true) . PHP_EOL;

        if ($this->sendProvider == self::SEND_PROVIDER_AUTO) {
            Sms::sendSms(...$this->params);
        } elseif ($this->sendProvider == self::SEND_PROVIDER_ALI) {
            $this->sendAliSms();
        }

    }

    public function setParam($aParam)
    {
        $this->params[0] = $aParam['iphone'];
        $this->params[1] = $aParam['aParam'];
        $this->params[2] = $aParam['aTempIds'];
        $this->params[3] = $aParam['IDDCode'];

        if (isset($aParam['aTempIds']['aliyun']) && isset($aParam['aTempIds']['CnUnicom'])) {
            $this->sendProvider = self::SEND_PROVIDER_AUTO;
        } elseif (isset($aParam['aTempIds']['aliyun'])) {
            $this->sendProvider = self::SEND_PROVIDER_ALI;
        } elseif ($aParam['aTempIds']['CnUnicom']) {
            $this->sendProvider = self::SEND_PROVIDER_CNUNION;
        }

    }

    public function getParam()
    {
        return $this->params;
    }

    //发送阿里云短信
    protected function sendAliSms()
    {
        $aAliyunTemp = ['aliyun' => ['template' => $this->params[2]['aliyun']]];
        Sms::sendTplMsg($this->params[0], $aAliyunTemp, $this->params[1], $this->params[3]);
    }

}